Page({
  data:{
    product_favorites:[],
    end: true,
    page_index: 1,
    page_size: 20,
    cur_product_id:0,
    isShow : false,
  },
  onLoad: function (options) {
    this.getMyFavorites()
    this.setData({
      isShow: false,
    })
  },
  getMyFavorites() {
    var url = getApp().data.url + 'product/getAttention';
    var body = {
      "page_index": this.data.page_index,
      "page_size": this.data.page_size
    };
    wx.showLoading({
      title: '加载中..',
    })
    getApp().wxAjax(this.getMyFavoritesData, url, body);
  },
  getMyFavoritesData(res) {
    var resObj = res.data.body;
    console.log(resObj)
    var product_favorites_old = this.data.product_favorites;
    product_favorites_old.push.apply(product_favorites_old, resObj.pro_list);
    this.setData({
      product_favorites: product_favorites_old,
      end: resObj.is_end==0?false:true,
      isShow: true,
    })
    wx.hideLoading();
  },
  onReachBottom: function () {
    var end = this.data.end;
    var page_index = this.data.page_index;
    this.setData({
      page_index: page_index + 1
    })
    if (!end) {
      this.getMyFavorites();
    }
  },
  towalk:function(event){
    // wx.navigateBack({
    //   url:'../my/my'
    // })
    wx.redirectTo({
      url: '../eIndex/eIndex'
    })
  },
  imageError:function (event) {
    console.log(event);
    var index = event.currentTarget.dataset.img;
    var product_favorites_old = this.data.product_favorites;
    product_favorites_old[index].main_image = "../image/seat_goods224x224@2x.png";
    this.setData({
      product_favorites: product_favorites_old
    })
  },
  tapCancelFavorite:function(event) {
    var productid = event.currentTarget.dataset.skuid;
    //console.log("product id = "+productid);
    var shopid = wx.getStorageSync('supermarket_id');
    this.data.productid = productid;
    var url = getApp().data.url + 'product/cancelAttention';
    var body = {
      "supermarket_id": shopid,
      "product_id": productid
    };
    wx.showLoading({
      title: '加载中..',
    })
    getApp().wxAjax(this.cancelFavorite, url, body);
  },
  cancelFavorite:function(res) {
    var resObj = res.data.body;
    console.log(resObj)
    var product_favorites_old = this.data.product_favorites;
    for (var i = 0; i < this.data.product_favorites.length;i++) {
      if (product_favorites_old[i].product_id == this.data.productid) {
        product_favorites_old.splice(i,1);
        break;
      }
    }
    this.setData({
      product_favorites: product_favorites_old
    });
    wx.hideLoading();
  },
  toPDetails(e) {
    var skuid = e.currentTarget.dataset.sku_ctgy;
    wx.navigateTo({
      url: '../pDetails/pDetails?skuid=' + skuid
    })
  }
})